﻿@inject IPopupService PopupService



<style>
    .option-card {
        border: 1px solid var(--bs-secondary);
    }

        .option-card:hover {
            border: 1px solid var( --main-color);
        }

    .option-card-active {
        border: 1px solid var( --main-color) !important;
    }
    .option-card-show {
        border: 1px solid var(--bs-secondary);
    }
    .option-background {
        background-color: var(--md-sys-color-primary-container);
    }
    .option-card-active .option-background {
        background-color: var(--main-color) !important;
        opacity: 0.5;
    }

</style>
<CnGalWebSite.Components.Containers.MasaTitleContainer Title="投票选项" Icon="@IconType.Vote.ToIconString()" Class="mb-4">
        <div class="mb-4">@( Model.Type==VoteType.MultipleChoice?(Model.MaximumSelectionCount==Model.MinimumSelectionCount?($"每人投{Model.MaximumSelectionCount}票"):($"最多投{Model.MaximumSelectionCount}票，最少投{Model.MinimumSelectionCount}票")):"每人只能投1票")</div>
    @if (IsShowResult)
    {
        @foreach (var item in Model.Options)
        {
            <div class="@("mb-2 "+((Model.Options.MaxBy(s=>s.Count).Count==item.Count)?"option-card-active":"option-card-show"))" style="border-radius: 5px; ">
                <OptionAloneCard TotalCount="Model.Count" IsShowResult="IsShowResult" Model="item" IsSeleted="@(Model.UserSelections.Contains(item.OptionId))"
                                 ImageMinWidth="@ImageMinWidth"/>
            </div>
        }
    }
    else
    {
        <div>
            @foreach (var item in Model.Options)
            {
                <div class="@(Model.UserSelections.Contains(item.OptionId)?"mb-2 option-card-active":"mb-2 option-card")" style=" display: flex; flex-direction: row; border-radius: 5px; cursor:pointer"
                     @onclick="@(()=>OnSeletedItemChanged(item,Model.UserSelections.Contains(item.OptionId)))">
                    <div style=" display: flex; flex-direction: column; justify-content: center; " class=" ms-3">
                        <MCheckbox TValue="bool" Value="@(Model.UserSelections.Contains(item.OptionId))" ValueChanged="@((e)=>OnSeletedItemChanged(item,e))"
                                  Disabled="@(Model.UserSelections.Contains(item.OptionId)?false:(Model.UserSelections.Count>=Model.MaximumSelectionCount))"></MCheckbox>
                    </div>
                    <OptionAloneCard TotalCount="Model.Count" IsShowResult="IsShowResult" Model="item" IsSeleted="@(Model.UserSelections.Contains(item.OptionId))"
                                     ImageMinWidth="@ImageMinWidth"/>
                </div>
            }
        </div>
    }
</CnGalWebSite.Components.Containers.MasaTitleContainer>

@code {
    [Parameter]
    public VoteViewModel Model { get; set; }
    [Parameter]
    public bool IsShowResult { get; set; }

    public string ImageMinWidth { get; set; }


    protected override async Task OnInitializedAsync()
    {
        if(Model.Options.Any(s=>string.IsNullOrWhiteSpace(s.Image)==false))
        {
            if (Model.Entries.Any(s => s.Type == EntryType.Game || s.Type == EntryType.ProductionGroup) || Model.Artciles.Count > 0 || Model.Peripheries.Count > 0)
            {
                ImageMinWidth = "min-width:107px;";
            }
            else
            {

                ImageMinWidth = "min-width:50px;";
            }
        }
        else
        {

        }
    }

    public List<long> GetSeletedItems()
    {
        return Model.UserSelections;
    }

    public async Task OnSeletedItemChanged(VoteOptionViewModel item, bool state)
    {
        if (state)
        {
            if (Model.Type == VoteType.SingleChoice)
            {
                Model.UserSelections.Clear();
            }
            else
            {
                if (Model.UserSelections.Count == Model.MaximumSelectionCount)
                {
                    await PopupService.ToastErrorAsync($"最多选择{Model.MaximumSelectionCount}项", $"最多选择{Model.MaximumSelectionCount}项");
                    StateHasChanged();
                    return;
                }
            }
            Model.UserSelections.Add(item.OptionId);
        }
        else
        {
            Model.UserSelections.Remove(item.OptionId);
        }


    }
}
